package com.nit.lab.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.context.event.ContextStoppedEvent;


/**
 * Created by wujiao on 2017/7/6.
 */
public final class SpringTool implements ApplicationContextAware,ApplicationListener {
    private static ApplicationContext applicationContext = null;


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (SpringTool.applicationContext == null) {
            SpringTool.applicationContext = applicationContext;
            System.out.println(
                    "========ApplicationContext配置成功,在普通类可以通过调用ToolSpring.getAppContext()获取applicationContext对象,applicationContext="
                            + applicationContext + "========");
        }
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        //如果容器关闭时，触发
        if (event instanceof ContextClosedEvent) {
            ContextClosedEvent cce = (ContextClosedEvent) event;
            System.out.println("#####################");
            System.out.println("容器关闭");
            System.out.println(cce);
            System.out.println("#####################");
        }
        //容器刷新时候触发
        if (event instanceof ContextRefreshedEvent) {
            ContextRefreshedEvent cre = (ContextRefreshedEvent) event;
            System.out.println("#####################");
            System.out.println("容器刷新");
            System.out.println(cre);
            System.out.println("#####################");
            try {
//                LoginUtil.getDate();
//                AlarmUtil.getAlarm();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //容器启动的时候触发
        if (event instanceof ContextStartedEvent) {
            ContextStartedEvent cse = (ContextStartedEvent) event;
            System.out.println("#####################");
            System.out.println("容器启动");
            System.out.println(cse);
            System.out.println("#####################");
        }
        //容器停止时候触发
        if (event instanceof ContextStoppedEvent) {
            ContextStoppedEvent cse = (ContextStoppedEvent) event;
            System.out.println("#####################");
            System.out.println("容器停止");
            System.out.println(cse);
            System.out.println("#####################");
        }
    }
}